/**
 * Created by LWY on 2017/10/23.
 */
document.write('<meta charset="utf-8">');
document.write('<meta name="renderer" content="webkit|ie-comp|ie-stand">');
document.write('<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">');
document.write('<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/> ');
document.write('<meta http-equiv="Cache-Control" content="no-siteapp"/>');
document.write('<meta name="keywords" content="爱熊宝后台管理系统">');
document.write('<meta name="description" content="爱熊宝后台管理系统">');


document.write('<link href="static/h-ui/css/H-ui.min.css" rel="stylesheet" type="text/css"/>');
document.write('<link rel="stylesheet" type="text/css" href="static/h-ui.admin/css/H-ui.admin.css" />');
document.write('<link href="static/h-ui.admin/css/H-ui.login.css" rel="stylesheet" type="text/css"/>');
document.write('<link href="static/h-ui.admin/css/style.css" rel="stylesheet" type="text/css"/>');
document.write('<link href="lib/Hui-iconfont/1.0.8/iconfont.css" rel="stylesheet" type="text/css"/>');
document.write('<link rel="stylesheet" type="text/css" href="static/h-ui.admin/skin/default/skin.css" id="skin" />');
document.write('<link rel="stylesheet" type="text/css" href="../js/bootstrap/css/bootstrap.css" id="skin" />');
document.write('<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.css">');

document.write('<script type="text/javascript" src="lib/jquery/1.9.1/jquery.min.js"></script>');
document.write('<script type="text/javascript" src="static/h-ui/js/H-ui.min.js"></script>');
document.write('<script type="text/javascript" src="lib/layer/2.4/layer.js"></script>');
document.write('<script type="text/javascript" src="static/h-ui.admin/js/H-ui.admin.js"></script>');
<!--菜单-->
document.write('<script type="text/javascript" src="lib/jquery.contextmenu/jquery.contextmenu.r2.js"></script>');
//IE 9
document.write('<script type="text/javascript" src="lib/html5shiv.js"></script>');
document.write('<script type="text/javascript" src="lib/respond.min.js"></script>');
//IE6
document.write('<script type="text/javascript" src="lib/DD_belatedPNG_0.0.8a-min.js"></script>');
//md5
document.write('<script type="text/javascript" src="../js/md5.js"></script>');
//table相关
document.write('<script type="text/javascript" src="lib/My97DatePicker/4.8/WdatePicker.js"></script>');
document.write('<script type="text/javascript" src="lib/datatables/1.10.0/jquery.dataTables.min.js"></script>');
//表单验证
document.write('<script type="text/javascript" src="../js/Validform_v5.3.2_min.js"></script>');
//bootstrap-table
document.write('<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>');
document.write('<script src="//cdnjs.cloudflare.com/ajax/libs/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js"></script>');
//bootstrap-table  导出
document.write('<script type="text/javascript" src="../js/bootstrap/bootstrap-table-export.js"></script>');
document.write('<script type="text/javascript" src="../js/bootstrap/tableExport.js"></script>');

//laydate  日期时间
document.write('<script type="text/javascript" src="../js/laydate/laydate.js"></script>');

/*document.write('<script>DD_belatedPNG.fix(' * ');</script>');*/


/**
 * 提示并且延迟做后续操作
 * @param msg  提示信息
 * @param fun  函数
 * @param delay  延迟毫秒数
 */
function msgAndAfter(msg, fun, delay) {
    if (msg) {
        layer.msg(msg);
    }
    if (fun != null) {
        if (delay) {
            setTimeout(function () {
                fun();
            }, delay);
        } else {
            setTimeout(function () {
                fun();
            });
        }
    }

}

/**
 * 获取工程路径
 * @returns {string}
 */
function getContextPath() {
    var pathName = document.location.pathname;
    var index = pathName.substr(1).indexOf("/");
    var result = pathName.substr(0, index + 1);
    return result;
}

/**
 * 获取url参数
 * @param variable   key
 * @returns {boolean}
 */
function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
            return decodeURI(pair[1]);
        }
    }
    return "";
}

/**
 * 请求服务器
 * @param url   url地址
 * @param type  请求类型 get或者post
 * @param async 是否异步
 * @param data  参数
 * @param success_callback  成功回调
 * @param error_callback  错误回调
 */
function requestService(url, type, async, data, success_callback, error_callback) {
    $.ajax({
        url: getContextPath() + url,
        type: type,
        async: async,
        data: data,
        success: function (result) {
            var code = result.code;
            var desc = result.desc;
            if (code == 200) {
                if (success_callback != null) {
                    success_callback(result);
                }
            } else {
                layer.msg(desc)
            }
        },
        error: function () {
            if (error_callback != null) {
                error_callback();
            } else {
                layer.msg("出错啦！");
            }
        }
    });
}

/**
 * 加载按钮
 * @param p_id  菜单id
 * @param div_id  页面按钮块div_id
 */
function loadButton(p_id, div_id) {
    requestService("/permission/userinfo/button/" + p_id, "get", true, "", function (result) {
        var html = '';
        $.each(result.data, function (index, row) {
            html += '<button id="btn_' + row.method + '" type="button" class="btn btn-default"';
            html += ' onclick="' + row.method + '()">';
            html += '<span class="glyphicon ' + row.icon_url + '" aria-hidden="true"></span>' + row.permission_name;
            html += '</button>';
        });
        $('#' + div_id).append(html);
    });
}

/**
 * 加载数据
 * @param url
 */
function loadData(url, serializeArray, columns) {
    //先销毁表格
    $('#table').bootstrapTable('destroy');
    $('#table').bootstrapTable({
        url: getContextPath() + url,
        method: 'post',
        contentType: "application/x-www-form-urlencoded",
        queryParamsType: '',
        search: false,
        showRefresh: false,
        showExport: true,
        exportDataType: 'all',
        toolbar: '#toolbar',
        clickToSelect: true,
        queryParams: function queryParams(params) {
            var param = {
                pageNum: params.pageNumber,
                pageSize: params.pageSize
            };
            $.each(serializeArray, function (i, r) {
                param[r.name] = r.value;
            });
            return param;
        },
        striped: true,
        pagination: true,
        pageNumber: 1,//初始化加载第一页，默认第一页
        pageSize: 15,//每页的记录行数（*）
        pageList: [5, 10, 15, 20, 25],  //记录数可选列表
        sidePagination: "server",
        responseHandler: function (res) {
            var table_data = {};
            table_data.total = res.data.total;
            table_data.rows = res.data.list;
            return table_data;
        },
        columns: columns
    });
}

/**
 * 搜索
 */
function search(url) {
    loadData(url, $("#form").serializeArray(), columns);
}

/**
 * 操作（del： 删除）
 * @type {{del: op.del}}
 */
var op = {
    del: function (url) {
        var rows = $('#table').bootstrapTable('getSelections');
        if (rows.length == 0) {
            layer.msg("没有选择");
            return;
        }
        layer.confirm('确认要删除吗？', function () {
            var ids = [];
            $.each(rows, function (index, row) {
                ids.push(row.id);
            });
            requestService(url, "post", true, {ids: ids}, function (result) {
                msgAndAfter(result.desc, function () {
                    $('#table').bootstrapTable('refresh')
                }, 500);
            }, null);
        });
    },
    edit: function (title, url, w, h) {
        var rows = $('#table').bootstrapTable('getSelections');
        if (rows.length == 0) {
            layer.msg("没有选择");
            return;
        }
        if (rows.length > 1) {
            layer.msg("最多只能选择一个节点");
            return;
        }
        var row = rows[0];
        url += "?id=" + row.id;
        window.sessionStorage.setItem("row", JSON.stringify(row));
        layer_show(title, url, w, h);
    },
    add: function (title, url, w, h) {
        layer_show(title, url, w, h);
    }
};

